<?php
namespace Particle\Plugin;
use Particle\Core;

class main extends Core\Controller {

    function __construct() {
        
        parent::__construct();
                
    }
    
    function runSCORM($SCOInstanceID, $SCOStudentName, $SCOStudentID, $SCORMZipFile) {
        
        $SCOInstanceID = (int)$SCOInstanceID * 1;
        
        if( class_exists('ZipArchive') ){
            
            ///** upload file **///
            
            $resource = new ZipArchive(); 
            
            $resource->open($SCORMZipFile);
            
            $fp = $resource->getStream('imsmanifest.xml');
            if(!$fp) die('Invalido SCORM Imsmanifest');
            
            /* envia al modelo y graba la actividad id 20 */
            
            $resource->extractTo('resources'.DS.'scorm'.DS.'data'.DS.'20');
            
            $fileImsmanifest = fopen('resources'.DS.'scorm'.DS.'data'.DS.'20'.DS.'imsmanifest.xml', 'r');
            if(!$fileImsmanifest) die('Invalido SCORM Imsmanifest');
            
            $contents = '';
            
            while (!feof($fileImsmanifest)) {
                $contents .= fread($fileImsmanifest, 2);
            }

            $imsmanifest = simplexml_load_string($contents);

            $index = (string)$imsmanifest->resources->resource->attributes()->href;

            $url = 'resources/scorm/data/20/'.$index;

            if(isset($url) && !empty($url)){

                /// envio url a template player ////

            }else{
                die('No se puede leer index scorm');
            }

        }else{
            die('Class Zip is not available');
        }

    }
    
    
}